cc.Class({
    extends: cc.Component,

    properties: 
    {
        _ControlNode: null,   // 控制节点
        ControlNode:
        {
            get: function()
            {
                return this._ControlNode;
            },
            set: function(val)
            {
                this._ControlNode = val;
            },
            type: cc.Node,
        },
        
        _NumLabel: null,   // 数量文字
        NumLabel:
        {
            get: function()
            {
                return this._NumLabel;
            },
            set: function(val)
            {
                this._NumLabel = val;
            },
            type: cc.Label,
        },
       
        _MaxSolider: 20,          // 最大屯兵数
        MaxSoilder:
        {
            get: function()
            {
                return this._MaxSolider;
            },
            set: function(val)
            {
                this._MaxSolider = val;
            }
        },

        _ReduceInterval: 0.2,  // 屯兵数量减少间隔
        ReduceInterval:
        {
            get: function()
            {
                return this._ReduceInterval;
            },
            set: function(val)
            {
                this._ReduceInterval = val;
            }
        },

        _ReduceTime: 0,        // 减少兵的时间
        ReduceTime:
        {
            get: function()
            {
                return this._ReduceTime;
            },
            set: function(val)
            {
                this._ReduceTime = val;
            }
        },

        _CurSoliderNum: 0,     // 当前兵数量
        CurSoilderNum:
        {
            get: function()
            {
                return this._CurSoliderNum;
            },
            set: function(val)
            {
                this._CurSoliderNum = val;
                this.updateSoiderNum();
            }
        },

        _Type: 1,   // 1我方，2敌方，3中立
        Type:
        {
            get: function()
            {
                return this._Type;
            },
            set: function(val)
            {
                this._Type = val;
            }
        },

        _ProduceInterval: 0.2,    // 生产兵间隔
        ProduceInterval:
        {
            get: function()
            {
                return this._ProduceInterval;
            },
            set: function(val)
            {
                this._ProduceInterval = val;
            }
        },

        _ProduceTime: 0,          // 生产兵的时间
        ProduceTime:
        {
            get: function()
            {
                return this._ProduceTime;
            },
            set: function(val)
            {
                this._ProduceTime = val;
            }
        },

        _Category: 1,       // 1基地，2兵营，3塔
        Category:
        {
            get: function()
            {
                return this._Category;
            },
            set: function(val)
            {
                this._Category = val;
            }
        },
    },

    onLoad() 
    {
        this.NumLabel = this.node.getChildByName("label").getComponent("cc.Label");
        this.ControlNode = cc.find("Canvas/ControlNode");
    },

    start() 
    {

    },

    update(dt) 
    {
        // 屯兵超过上限，则减少兵的数量
        if(this.CurSoilderNum > this.MaxSoilder)
        {
            if (this.ReduceTime > this.ReduceInterval) 
            {
                this.ReduceTime = 0;
                this.changeSoliderNum(-1);
            }
            this.ReduceTime = this.ReduceTime+dt;
        }
        if(this.Category == 1)
        {
            // 基地生产兵
            if(this.CurSoilderNum < this.MaxSoilder) 
            {
                if(this.ProduceTime > this.ProduceInterval) 
                {
                    this.ProduceTime = 0;
                    this.changeSoliderNum(1);
                }
                this.ProduceTime = this.ProduceTime+dt;
            }
        }
    },

    // 修改兵的数量
    changeSoliderNum(num)
    {
        this.CurSoilderNum = Math.max(0, this.CurSoilderNum+num);
        this.updateSoiderNum();
    },


    // 更新兵数量文字
    updateSoiderNum() 
    {
        this.NumLabel.string = this.CurSoilderNum+"/"+this.MaxSoilder;
    },
});
